﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.person_info
{
    public partial class group_set : BasePage

    {
        private string GetDeptName(SqlConnection conn, int parentDept, string deptName)
        {
            if (parentDept != 0)
            {
                this.BindDeptTree(conn, parentDept, ref deptName);
            }
            return deptName;
        }
        private void BindDeptTree(SqlConnection conn, int deptId, ref string deptName)
        {
            string commandText = "SELECT [ParentDept],[DeptName] FROM [dbo].[Department] WHERE [DeptId] = " + deptId.ToString();
            DataTable table = SqlHelper.ExecuteDataset(conn, CommandType.Text , commandText).Tables[0];
            if (table.Rows.Count > 0)
            {
                deptName = table.Rows[0]["DeptName"].ToString() + "/" + deptName;
                if (Convert.ToInt32(table.Rows[0]["ParentDept"].ToString()) != 0)
                {
                    this.BindDeptTree(conn, Convert.ToInt32(table.Rows[0]["ParentDept"].ToString()), ref deptName);
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                int groupId = (Request.QueryString["group_id"] != null) ? int.Parse(Request.QueryString["group_id"].ToString()) : 0;
                this.ViewState["GroupId"] = groupId;
                string str = string.Empty;
                DataTable table = new GroupDao().SelectByGroupId(base.Conn, base.User.Identity.Name, groupId);
                if ((table.Rows.Count > 0) && (table.Rows[0]["UserStr"] != DBNull.Value))
                {
                    str = "," + table.Rows[0]["UserStr"].ToString() + ",";
                }
                DataTable table2 = new UsersDao().SelectBaseAll(base.Conn);
                if (table2.Rows.Count > 0)
                {
                    for (int i = 0; i < table2.Rows.Count; i++)
                    {
                        ListItem item = new ListItem("[" + this.GetDeptName(base.Conn, Convert.ToInt32(table2.Rows[i]["ParentDept"].ToString()), table2.Rows[i]["DeptName"].ToString()) + "] " + table2.Rows[i]["UserName"].ToString(), table2.Rows[i]["UserId"].ToString());
                        if (str.IndexOf("," + table2.Rows[i]["UserId"].ToString() + ",") >= 0)
                        {
                            this.pklOperator.ItemsLeft.Add(item);
                        }
                        else
                        {
                            this.pklOperator.ItemsRight.Add(item);
                        }
                    }
                }
            }
            if (this.Page.IsPostBack)
            {
                string userStr = "";
                foreach (ListItem item2 in this.pklOperator.ItemsLeft)
                {
                    userStr = userStr + item2.Value + ",";
                }
                new GroupDao().Set(base.Conn, base.User.Identity.Name, Convert.ToInt32(this.ViewState["GroupId"].ToString()), userStr);
                this.pnlMain.Visible = false;
                this.litMessage.Text = base.ShowPrompt("人员设置已保存", "group.aspx");
            }
        }

    }
}
